<?php
namespace Admin\Controller;

class OrderController extends CommonController{
	public $exec_comfunc = 'index,del';
	public $_db_order    = '';

	public function index(){
		//按关键字类型
		$kw_type = $this->_get('kw_type');
		$k_word = $this->_get('k_word');
		
		$this->assign('kw_type', $kw_type);
		$this->assign('k_word', $k_word);
		if($kw_type && $k_word){
			switch ($kw_type) {
				// 按订单号
				case 'order_sn':
					$where['order_sn'] = $k_word;
					break;
				// 按律师姓名
				case 'lawyer_name':
					$uwhere['realname']  = array('like','%' . $k_word . '%');
					$uwhere['is_lawyer'] = 1;
					$user_id_list = D('User')->where($uwhere)->getField('user_id', true);
					$user_id_str  = implode(',', $user_id_list ? $user_id_list : '');
					$where['lawyer_user_id'] = $user_id_str ? array('in', $user_id_str) : 1;
					break;
				// 按用户姓名
				case 'user_name':
					$uwhere['realname']  = array('like','%' . $k_word . '%');
					$uwhere['is_lawyer'] = 0;
					$user_id_list = D('User')->where($uwhere)->getField('user_id', true);
					$user_id_str  = implode(',', $user_id_list ? $user_id_list : '');
					$where['user_id'] = $user_id_str ? array('in', $user_id_str) : 1;
					break;

				//按用户手机号搜索
				case 'mobile':
					$uwhere['mobile'] = $k_word;
					$where['user_id'] = D('User')->where($uwhere)->getField('user_id');
					break;

				//按用户ID
				case 'user_id':
					$where['user_id'] = $k_word;
					break;
			}
		}
		$title = $this->_get('title');
		if($title){
			$where['title'] = array('like', '%' . $title . '%');
		}
		//按开始价格
		$start_price = $this->_get('start_price');
		if($start_price){
			$where['order_amount'][] = array('egt', $start_price);
		}
		$this->assign('start_price', $start_price);

		//按结束价格
		$end_price   = $this->_get('end_price');
		if($end_price){
			$where['order_amount'][] = array('elt', $end_price);
		}
		$this->assign('end_price', $end_price);

		//按订单状态
		$status = $this->_get('status');
		if($status){
			$where['status'][] = array('eq', $status);
		}
		$this->assign('status', $status);

		//按订单类型
		$category_id = $this->_get('category_id');
		if($category_id){
			$where['category_id'] = array('eq', $category_id);
			$info['category_id']  = $category_id;
		}

		//时间类型
		$time_type = $this->_get('time_type');
		$this->assign('time_type', $time_type);
		switch ($time_type) {
			case 'today':
				$where['create_time'][] = array('egt', strtotime(date('Y-m-d')));
				break;

			case 'week':
				$where['create_time'][] = array('egt', strtotime('-1 week'));
				break;

			case 'month':
				$where['create_time'][] = array('egt', strtotime('-1 month'));
				break;

			case '3month':
				$where['create_time'][] = array('egt', strtotime('-3 month'));
				break;
		}
		
		//按开始日期
		$start_date = $this->_get('start_date');
		if($start_date){
			$where['create_time'][] = array('egt',strtotime($start_date));
		}
		$this->assign('start_date',$start_date);

		//按结束日期
		$end_date   = $this->_get('end_date');
		if($end_date){
			$where['create_time'][] = array('elt',strtotime($end_date.'23:59:59'));
		}
		$this->assign('end_date',$end_date);

		//省份
		$province_id = $this->_get('province_id');
		$info['province_id'] = $province_id;
		if($province_id && !$city_id){
			$where['province_id'] = $province_id;
		}
		//城市
		$city_id = $this->_get('city_id');
		$info['city_id'] = $city_id;
		if($city_id){
			$where['city_id'] = $city_id;
		}
		$this->assign('info', $info);
		//客服城市
		if($this->_city_id){
			$where['city_id'] = $this->_city_id;
		}

		// 客服及审核人员处理
		if(!D('Role')->is_acl('Order/service_confirm')){
			$is_service = 1;
		}
		if(!D('Role')->is_acl('Order/audit_confirm')){
			$is_audit = 1;
		}
		// 客服处理
		if($is_service && !$is_audit){
			$service_status_list = C('service_order_status_desc');
			foreach ($service_status_list as $key => $value) {
				if($value){
					$service_status_arr[] = $key;
				}
			}
			$where['status'][] = array('in', implode(',', $service_status_arr));
		}
		// 审核处理
		if(!$is_service && $is_audit){
			$audit_status_list = C('audit_order_status_desc');
			foreach ($audit_status_list as $key => $value) {
				if($value){
					$audit_status_arr[] = $key;
				}
			}
			$where['status'][] = array('in', implode(',', $audit_status_arr));
		}
		if($is_service && $is_audit){
			$is_view_all = 1;
		}
		$this->assign('is_view_all', $is_view_all);

		$export = $this->_get('export');
		if($export){
			$this->_show_all_data = true;
		}
		// var_dump($where);
		$list = parent::index('', $where, 'create_time desc', true);
		foreach ($list as $rs) {
			$data = D('Order')->filter_field($rs);
			$new_list[] = $data;
		}
		//显示内容名称
		$data_title = array(
			'order_sn'         => array('title'=>'订单编号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'category_name'    => array('title'=>'类别','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'user_id'          => array('title'=>'UserID','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'user_name'        => array('title'=>'姓名','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'user_mobile'      => array('title'=>'手机号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'title'            => array('title'=>'服务名称','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'order_sn'         => array('title'=>'订单号','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'order_amount'     => array('title'=>'订单金额','td_style'=>'','val_class'=>'price','val_pre'=>'','val_default'=>''),
			'payed_amount'     => array('title'=>'实付金额','td_style'=>'','val_class'=>'price','val_pre'=>'','val_default'=>''),
			'lawyer_name'      => array('title'=>'处理律师','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'datetime'         => array('title'=>'订单时间','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
			'user_status_desc' => array('title'=>'订单状态','td_style'=>'','val_class'=>'','val_pre'=>'','val_default'=>''),
		);
		if($export=='xls'){
			import("ExcelUtil", 'Public/Class/suncco/');
			ExcelUtil::export($data_title, $new_list, '用户订单');
		}
		unset($where);
		$where['is_delete'] = 0;
		$total_num = D('Order')->where($where)->count();
		$this->assign('total_num', $total_num);

		$where['create_time'] = array('egt', strtotime(date('Y-m-d')));
		$today_num = D('Order')->where($where)->count();
		$this->assign('today_num', $today_num);

		$this->assign('data_title', $data_title);
		$this->assign('table_width', '1200');
		$this->assign('list', $new_list);
		$this->display();
	}
	// 客服确认需求
	public function service_confirm(){
		$this->add_step('客服确认需求');
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info     = D('Order')->info($order_sn);
		if(IS_POST){
			//客服确认需求
			$agreement_file_ids = $this->_post('agreement_file_ids');
			if(!$agreement_file_ids[1]){
				$this->error('请上传合同文件！');
			}
			$lawyer_user_id = $this->_post('lawyer_user_id');
			if(!$lawyer_user_id[1]){
				$this->error('请选择律师！');
			}
			$data['status'] = 'wait_pay';//设置支付状态
			$data['arrange_lawyer_time'] = time();//安排律师时间
			// 订单操作记录
			D('Order')->opt_logs($order_sn, '客服确认需求', $this->_user_id, $this->_realname);
			$this->_db_where['order_sn'] = $order_sn;
			// 订单客服确认消息
			D('Message')->order_service_confirm($order_sn);
			parent::edit('', $data);
		}
		$this->assign('info', $info);
		$this->display();
	}
	// 设置备注
	public function win_set_remark(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		if(IS_POST){
			$data['remark'] = $this->_post('remark');
			D('Order')->where(array('order_sn' => $order_sn))->save($data);
			$this->success('设置备注成功！');
		}
		$this->assign('info', D('Order')->info($order_sn));
		$this->display();
	}

	// 审核确认
	public function audit_confirm(){
		$this->add_step('审核确认');
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info     = D('Order')->info($order_sn);
		if(IS_POST){
			$finish_file_ids = $this->_post('finish_file_ids');
			$audit_status    = $this->_post('audit_status');
			$audit_comment   = $this->_post('audit_comment');
			if($info['status'] == 'doing-audit'){
				$finish_file_ids = implode(',', $finish_file_ids);
				if(substr($finish_file_ids, 0, 2) == '0,'){
					$finish_file_ids = substr($finish_file_ids, 2);
				}
				$data['finish_file_ids'] = $finish_file_ids;
				$data['audit_status']    = $audit_status;
				$data['audit_comment']   = $audit_comment ? $audit_comment : '';
				$data['status']          = $audit_status == 'pass' ? 'finish' : 'doing-lawyer';//通过则完成订单，不通过则返回给律师处理
				$data['audit_time']      = time();//审核时间
				D('Order')->where(array('order_sn' => $order_sn))->save($data);
				if($audit_status == 'pass'){
					// 订单操作记录
					D('Order')->opt_logs($order_sn, '平台审核', $this->_user_id, $this->_realname);
					D('Order')->opt_logs($order_sn, '完成合同', $this->_user_id, $this->_realname);
					D('Message')->finish_order_msg($order_sn);
				}else{
					D('Order')->opt_logs($order_sn, '律师补充处理', $info['lawyer_user_id']);
				}
				$this->success('提交成功！', U('audit_confirm', array('order_sn' => $order_sn)));
			}else{
				$this->error('此订单状态下不能修改！');
			}
		}
		$is_comment = D('Comment')->get_comment_num($order_sn, 'audit_comment', $info['agreement_id']);
		$this->assign('is_comment', $is_comment);
		$this->assign('info', $info);
		$this->display();
	}
	// 审核人评价
	public function win_audit_comment(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info = D('Order')->info($order_sn);
		if(IS_POST){
			$label_id = implode(',', $form_data['label_id']);
			$content  = $this->_post('content');
			$finish_quality_star   = $this->_post('finish_quality_star');
			$working_speed_star    = $this->_post('working_speed_star');
			$service_attitude_star = $this->_post('service_attitude_star');
			D('Comment')->submit(
				'audit_comment',
				0,
				$order_sn,
				$content,
				'',
				$info['agreement_id'],
				$info['lawyer_user_id'],
				$finish_quality_star,
				$working_speed_star,
				$service_attitude_star,
				$label_id,
				$this->_user_id
			);

			$this->success('评价成功！', U('audit_confirm', array('order_sn' => $order_sn)));
		}
		$this->assign('info', $info);
		$this->display();
	}
	// 取消订单
	public function cancel(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		
		if(D('Order')->cancel($order_sn)){
			$this->success('取消订单成功！');
		}else{
			$this->error(D('Order')->tips_info);
		}
	}
	public function win_help_pay(){
		$order_sn = $this->_get('order_sn', '对不起，您的操作有误！');
		$info = D('Order')->info($order_sn);
		if(IS_POST){
			$trade_no     = $this->_post('trade_no', '请填写交易号！');
			$buyer_email  = $this->_post('buyer_email', '请填写支付账号！');
			$seller_email = $this->_post('seller_email', '请填写收款账号！');
			$total_fee    = $info['order_amount'];
			$buyer_id     = $info['user_id'];
			if(D('Order')->order_pay('help_pay', $order_sn, $trade_no, $total_fee, $buyer_email, $buyer_id, $seller_email)){
				$this->success('支付成功！');
			}
		}
		$this->assign('info', $info);
		$this->display();
	}
}





